﻿using ChuongtrinhTracnghiem.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace ChuongtrinhTracnghiem.View
{
    public partial class FormCauhoi : Form
    {
        public FormCauhoi()
        {
            InitializeComponent();
        }
        //SqlConnection sqlConn = new SqlConnection(Settings.Default["TracnghiemDataConnectionString"].ToString());
        string sqlConnectionString = Settings.Default["TracnghiemDataConnectionString"].ToString();

        DataTable ds = new DataTable();
        int i = 0;
        int themsua = 0;

        public void Display_Tooltip1()
        {
            //toolStripButton_Them.Enabled = true;
            //toolStripButton_Sua.Enabled = true;
            //toolStripButton_Boqua.Enabled = false;
            //toolStripButton_Xoa.Enabled = true;
            //toolStripButton_Tim.Enabled = true;
            //toolStripButton_Dau.Enabled = true;
            //toolStripButton_Toi.Enabled = true;
            //toolStripButton_Lui.Enabled = true;
            //toolStripButton_Cuoi.Enabled = true;
        }

        public void Display_Tooltip2()
        {
            //toolStripButton_Them.Enabled = false;
            //toolStripButton_Sua.Enabled = false;
            //toolStripButton_Boqua.Enabled = true;
            //toolStripButton_Xoa.Enabled = false;
            //toolStripButton_Tim.Enabled = false;
            //toolStripButton_Dau.Enabled = false;
            //toolStripButton_Toi.Enabled = false;
            //toolStripButton_Lui.Enabled = false;
            //toolStripButton_Cuoi.Enabled = false;
        }

        public void Display()
        {
            string s = "select * from TblCauhoi";
            Model.Laydulieu dl = new Model.Laydulieu();
            ds = dl.Dulieu(s);
            DataColumn[] dt = new DataColumn[1];
            dt[0] = ds.Columns[0];
            ds.PrimaryKey = dt; //Tạo khóa 
            this.textBox_Cauthu.Text = (i + 1).ToString();
            this.richTextBox_Cauhoi.Text = ds.Rows[i]["Cauhoi"].ToString();
            this.textBox_DapanA.Text = ds.Rows[i]["DapanA"].ToString();
            this.textBox_DapanB.Text = ds.Rows[i]["DapanB"].ToString();
            this.textBox_DapanC.Text = ds.Rows[i]["DapanC"].ToString();
            this.textBox_DapanD.Text = ds.Rows[i]["DapanD"].ToString();
            this.comboBox_Dapandung.Text = ds.Rows[i]["Caudung"].ToString();
            Model.Ketnoi.Boketnoi();
            Display_Tooltip1();
        }

        private void FormCauhoi_Load(object sender, EventArgs e)
        {
            LoadData();
            //Display();
            textBox_Cauthu.Text = "";
            textBox_Cauthu.Enabled = false;
            richTextBox_Cauhoi.Text = "";
            textBox_DapanA.Text = "";
            textBox_DapanB.Text = "";
            textBox_DapanC.Text = "";
            textBox_DapanD.Text = "";
            comboBox_Dapandung.Items.Clear();
            comboBox_Dapandung.Items.Add("A");
            comboBox_Dapandung.Items.Add("B");
            comboBox_Dapandung.Items.Add("C");
            comboBox_Dapandung.Items.Add("D");
        }

        private void toolStripButton_Dau_Click(object sender, EventArgs e)
        {
            i = 0;
            Display();
        }

        private void toolStripButton_Toi_Click(object sender, EventArgs e)
        {
            if (i < ds.Rows.Count - 1)
            {
                i++;
                Display();
            }

        }

        private void toolStripButton_Lui_Click(object sender, EventArgs e)
        {
            if (i > 0)
            {
                i--;
                Display();
            }

        }

        private void toolStripButton_Cuoi_Click(object sender, EventArgs e)
        {
            i = ds.Rows.Count - 1;
            Display();

        }

        private void toolStripButton_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        int AddCauHoi(string CauHoi, string DapAnA, string DapAnB, string DapAnC, string DapAnD, string CauDung, string fileName)
        {
            bool Image = false;
            if (txtImage.Text != "")
            {
                Image = false;
            }
            else
            {
                Image = true;
            }

            if (Image == false)
            {
                byte[] imageData = null;

                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    imageData = new Byte[fs.Length];
                    fs.Read(imageData, 0, (int)fs.Length);
                }

                using (SqlConnection SQLconn = new SqlConnection(sqlConnectionString))
                {
                    SQLconn.Open();
                    string query = "insert into TblCauHoi (CauHoi, DapAnA, DapAnB, DapAnC, DapAnD, CauDung, HinhAnh) values(@CauHoi, @DapAnA, @DapAnB, @DapAnC, @DapAnD, @CauDung, @HinhAnh)";
                    SqlCommand sqlCommand = new SqlCommand(query, SQLconn);

                    sqlCommand.CommandType = CommandType.Text;

                    sqlCommand.Parameters.AddWithValue("@CauHoi", CauHoi);
                    sqlCommand.Parameters["@CauHoi"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.Add("@HinhAnh", SqlDbType.Image);
                    sqlCommand.Parameters["@HinhAnh"].Direction = ParameterDirection.Input;
                    // Lưu trữ mảng byte vào cột Image
                    sqlCommand.Parameters["@HinhAnh"].Value = imageData;

                    sqlCommand.Parameters.AddWithValue("@DapAnA", DapAnA);
                    sqlCommand.Parameters["@DapAnA"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@DapAnB", DapAnB);
                    sqlCommand.Parameters["@DapAnB"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@DapAnC", DapAnC);
                    sqlCommand.Parameters["@DapAnC"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@DapAnD", DapAnD);
                    sqlCommand.Parameters["@DapAnD"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@CauDung", CauDung);
                    sqlCommand.Parameters["@CauDung"].Direction = ParameterDirection.Input;

                    //--thuc hien xoa trắng các textbox 
                    textBox_Cauthu.Text = "";
                    textBox_Cauthu.Enabled = false;
                    richTextBox_Cauhoi.Text = "";
                    textBox_DapanA.Text = "";
                    textBox_DapanB.Text = "";
                    textBox_DapanC.Text = "";
                    textBox_DapanD.Text = "";
                    comboBox_Dapandung.Items.Clear();
                    comboBox_Dapandung.Items.Add("A");
                    comboBox_Dapandung.Items.Add("B");
                    comboBox_Dapandung.Items.Add("C");
                    comboBox_Dapandung.Items.Add("D");

                    MessageBox.Show("Câu hỏi đã được lưu !!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    return sqlCommand.ExecuteNonQuery();
                }
            }
            else
            {
                using (SqlConnection SQLconn = new SqlConnection(sqlConnectionString))
                {
                    SQLconn.Open();
                    string query = "insert into TblCauHoi (CauHoi, DapAnA, DapAnB, DapAnC, DapAnD, CauDung) values(@CauHoi, @DapAnA, @DapAnB, @DapAnC, @DapAnD, @CauDung)";
                    SqlCommand sqlCommand = new SqlCommand(query, SQLconn);

                    sqlCommand.CommandType = CommandType.Text;

                    sqlCommand.Parameters.AddWithValue("@CauHoi", CauHoi);
                    sqlCommand.Parameters["@CauHoi"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@DapAnA", DapAnA);
                    sqlCommand.Parameters["@DapAnA"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@DapAnB", DapAnB);
                    sqlCommand.Parameters["@DapAnB"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@DapAnC", DapAnC);
                    sqlCommand.Parameters["@DapAnC"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@DapAnD", DapAnD);
                    sqlCommand.Parameters["@DapAnD"].Direction = ParameterDirection.Input;

                    sqlCommand.Parameters.AddWithValue("@CauDung", CauDung);
                    sqlCommand.Parameters["@CauDung"].Direction = ParameterDirection.Input;

                    //--thuc hien xoa trắng các textbox 
                    textBox_Cauthu.Text = "";
                    textBox_Cauthu.Enabled = false;
                    richTextBox_Cauhoi.Text = "";
                    textBox_DapanA.Text = "";
                    textBox_DapanB.Text = "";
                    textBox_DapanC.Text = "";
                    textBox_DapanD.Text = "";
                    comboBox_Dapandung.Items.Clear();
                    comboBox_Dapandung.Items.Add("A");
                    comboBox_Dapandung.Items.Add("B");
                    comboBox_Dapandung.Items.Add("C");
                    comboBox_Dapandung.Items.Add("D");

                    MessageBox.Show("Câu hỏi đã được lưu !!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);


                    return sqlCommand.ExecuteNonQuery();
                }
            }
        }

        private void toolStripButton_Boqua_Click(object sender, EventArgs e)
        {
            Display();
            textBox_Cauthu.Enabled = false;
        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPG|*.jpg|PNG|*.png|GIF|*.gif|All Files (*.*)|*.*";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                pictureBox1.ImageLocation = openFileDialog1.FileName;
                txtImage.Text = openFileDialog1.FileName;
                openFileDialog1.Dispose();
            }
        }

        public void LoadData()
        {
            using (SqlConnection conn = new SqlConnection(sqlConnectionString))
            {
                DataTable table = new System.Data.DataTable();
                SqlCommand cmd = new SqlCommand("SELECT * FROM TblCauhoi", conn);
                cmd.CommandType = CommandType.Text;
                conn.Open();
                table.Load(cmd.ExecuteReader());
                dataGridView.DataSource = table;
                dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridView.ColumnHeadersDefaultCellStyle.Font = new Font ("Tahoma", 9.75F, FontStyle.Bold);
                dataGridView.Columns[0].HeaderText = "STT";
                dataGridView.Columns[0].Width = 35;
                dataGridView.Columns[1].HeaderText = "Nội Dung Câu Hỏi";
                dataGridView.Columns[1].Width = 300;
                dataGridView.Columns[2].HeaderText = "Đáp Án A";
                dataGridView.Columns[2].Width = 150;
                dataGridView.Columns[3].HeaderText = "Đáp Án B";
                dataGridView.Columns[3].Width = 150;
                dataGridView.Columns[4].HeaderText = "Đáp Án C";
                dataGridView.Columns[4].Width = 150;
                dataGridView.Columns[5].HeaderText = "Đáp Án D";
                dataGridView.Columns[5].Width = 150;
                dataGridView.Columns[6].HeaderText = "Đáp Án Đúng";
                dataGridView.Columns[6].Width = 150;
                dataGridView.Columns[7].HeaderText = "Hình Ảnh";
                dataGridView.Columns[7].Width = 150;
            }
        }

        /// <summary>
        /// Hàm chuyển byte sang Image
        /// </summary>
        /// <param name="byteArrayIn"></param>
        /// <returns></returns>
        public Image ByteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }

        private void Them_Click(object sender, EventArgs e)
        {
            int rs = AddCauHoi(richTextBox_Cauhoi.Text, textBox_DapanA.Text, textBox_DapanB.Text, textBox_DapanC.Text, textBox_DapanD.Text, comboBox_Dapandung.Text, txtImage.Text);
            if (rs > 0)
            {
                MessageBox.Show("OK");
                LoadData();
            }
            else MessageBox.Show("ERROR");
        }

        private void Sua_Click(object sender, EventArgs e)
        {
            textBox_Cauthu.Text = dataGridView.CurrentRow.Cells[0].Value.ToString();
            richTextBox_Cauhoi.Text = dataGridView.CurrentRow.Cells[1].Value.ToString();
            textBox_DapanA.Text = dataGridView.CurrentRow.Cells[2].Value.ToString();
            textBox_DapanB.Text = dataGridView.CurrentRow.Cells[3].Value.ToString();
            textBox_DapanC.Text = dataGridView.CurrentRow.Cells[4].Value.ToString();
            textBox_DapanD.Text = dataGridView.CurrentRow.Cells[5].Value.ToString();
            textBox_DapanD.Text = dataGridView.CurrentRow.Cells[6].Value.ToString();

            try
            {
                Image myImage = ByteArrayToImage((byte[])dataGridView.CurrentRow.Cells[7].Value);
                pictureBox1.Image = myImage;
            }
            catch
            {
                pictureBox1.Image = null;
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            textBox_Cauthu.Text = string.Empty;
            richTextBox_Cauhoi.Text = string.Empty;
            textBox_DapanA.Text = string.Empty;
            textBox_DapanB.Text = string.Empty;
            textBox_DapanC.Text = string.Empty;
            textBox_DapanD.Text = string.Empty;
            comboBox_Dapandung.Text = string.Empty;
        }
    }
}
